package com.juyu.miliao.api.rabbitMQ;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.juyu.miliao.common.constant.UserVipMqKey;
import com.juyu.miliao.common.domain.dto.UserVipMqMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author xuanqicheng
 */
@Component
@Slf4j
public class UserVipMq {
    @Autowired
    private AmqpTemplate amqpTemplate;
    @Autowired
    private ObjectMapper objectMapper;

    public void sendUserVipMqMessage(UserVipMqMessage userVipMqMessage){
        try {
            if (userVipMqMessage != null) {
                amqpTemplate.convertAndSend(UserVipMqKey.USER_VIP_INFO_EXCHANGE_KEY,
                        UserVipMqKey.USER_VIP_INFO_ROUTING_KEY, objectMapper.writeValueAsString(userVipMqMessage));
            }
        }catch (Exception ex){
            log.info("将user_vip信息放入mq中错误:"+ JSONObject.toJSONString(userVipMqMessage));
        }
    }
}
